package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysVoucherItemMapper;
import com.settlement.system.model.entity.SysVoucherItem;
import com.settlement.system.model.form.SysVoucherItemForm;
import com.settlement.system.model.query.SysVoucherItemQuery;
import com.settlement.system.model.vo.SysVoucherItemImportVo;
import com.settlement.system.service.SysVoucherItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 凭证明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-26
 */
@Service
public class SysVoucherItemServiceImpl extends ServiceImpl<SysVoucherItemMapper, SysVoucherItem> implements SysVoucherItemService {

    @Override
    public Page<SysVoucherItem> getPage(SysVoucherItemQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysVoucherItem> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysVoucherItem(SysVoucherItemForm sysVoucherItemForm) {
        SysVoucherItem sysVoucherItem = new SysVoucherItem();
        BeanUtils.copyProperties(sysVoucherItemForm, sysVoucherItem);
        sysVoucherItem.setCreateTime(null);
        return this.saveOrUpdate(sysVoucherItem);
    }

    @Override
    public SysVoucherItemForm getSysVoucherItemFormData(Long id) {
        SysVoucherItem sysVoucherItem = this.getById(id);
        SysVoucherItemForm sysVoucherItemForm = new SysVoucherItemForm();
        BeanUtils.copyProperties(sysVoucherItem, sysVoucherItemForm);
        return sysVoucherItemForm;
    }

    @Override
    public boolean deleteSysVoucherItem(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysVoucherItemImportVo> voList) {
        List<SysVoucherItem> sysVoucherItemList = new ArrayList<>();
        for (SysVoucherItemImportVo importVo : voList) {
            SysVoucherItem sysVoucherItem = new SysVoucherItem();
            BeanUtils.copyProperties(importVo, sysVoucherItem);
            sysVoucherItemList.add(sysVoucherItem);
        }
        this.saveOrUpdateBatch(sysVoucherItemList);
    }

    @Override
    public List<SysVoucherItemImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysVoucherItem> sysVoucherItemList = this.getBaseMapper().selectBatchIds(ids);
        List<SysVoucherItemImportVo> voList = new ArrayList<>();
        for (SysVoucherItem sysVoucherItem : sysVoucherItemList) {
            SysVoucherItemImportVo vo = new SysVoucherItemImportVo();
            BeanUtils.copyProperties(sysVoucherItem, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<SysVoucherItem> getItemList(Long voucherId) {
        return this.list(new LambdaQueryWrapper<SysVoucherItem>().eq(SysVoucherItem::getVoucherId, voucherId));
    }
}
